
prompt1="这是一个ACM的编程题，现在请你先复述一遍题目和测试数据，按题目原本的描述进行描述，然后在进行python编程解答"
#必须选择gpt-5作为model。如果没改对，告诉他示例没有完全通过

prompt2="""
示例没有完全通过，输入输出改写成
n = input()      #指定为N个数，输入,表示第一行的输入
lst = []         #指定一个空列表
for i in range(int(n)):        #循环N次
    lst.append(int(input()))      #不断的调用接下来行的输入

"""

# 按 acm 输入输出规则改写
# 参考 @file_context_0
def main():
    n = input().strip()
    if not n:
        print(0)
        return
    text = n
    n = int(input())
    word_score = {}
    for _ in range(n):
        w, s = input().split()
        s = int(s)
        if w in word_score:
            if s > word_score[w]:
                word_score[w] = s
        else:
            word_score[w] = s

    m = int(input())
    trans_raw = {}
    for _ in range(m):
        a, b, v = input().split()
        v = int(v)
        key = (a, b)
        if key in trans_raw:
            if v > trans_raw[key]:
                trans_raw[key] = v
        else:
            trans_raw[key] = v

    if not word_score:
        print(0)
        return

    words = list(word_score.keys())
    wid = {w: i for i, w in enumerate(words)}
    base = [0] * len(words)
    for w, s in word_score.items():
        base[wid[w]] = s

    incoming = [dict() for _ in range(len(words))]
    for (a, b), v in trans_raw.items():
        if a in wid and b in wid:
            ai, bi = wid[a], wid[b]
            old = incoming[bi].get(ai)
            if old is None or v > old:
                incoming[bi][ai] = v

    children = [{}]
    outs = [[]]
    for w, i in wid.items():
        node = 0
        for ch in w:
            if ch not in children[node]:
                children[node][ch] = len(children)
                children.append({})
                outs.append([])
            node = children[node][ch]
        outs[node].append(i)

    L = len(text)
    matches_by_end = [[] for _ in range(L + 1)]

    for s in range(L):
        node = 0
        for e in range(s, L):
            ch = text[e]
            if ch not in children[node]:
                break
            node = children[node][ch]
            if outs[node]:
                for i in outs[node]:
                    matches_by_end[e + 1].append((s, i))

    NEG = float("-inf")
    dp = [None] * (L + 1)

    for e in range(1, L + 1):
        if not matches_by_end[e]:
            continue
        cur_map = {}
        for s, w2 in matches_by_end[e]:
            best = NEG
            add = base[w2]
            if s == 0:
                best = max(best, add)
            prev_map = dp[s]
            if prev_map is not None:
                inc = incoming[w2]
                for p, bonus in inc.items():
                    v = prev_map.get(p)
                    if v is not None:
                        best = max(best, v + add + bonus)
                if len(prev_map) > 0:
                    for p, v in prev_map.items():
                        if p not in inc:
                            if v + add > best:
                                best = v + add
            if best != NEG:
                old = cur_map.get(w2)
                if old is None or best > old:
                    cur_map[w2] = best
        if cur_map:
            dp[e] = cur_map

    ans_map = dp[L]
    print(max(ans_map.values()) if ans_map else 0)


#测试数据
"""
aababa
4
a 1
aa 3
ab 2
ba 2
3
aa ba 2
ba ba -1
ab a 1
预期输出：
8
"""

if __name__ == "__main__":
    main()